﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace HZ.WinForm
{
    /// <summary>
    /// WinForm页面的分页控件
    /// </summary>
    /// <example>
    /// 代码示例,具体可参见WinFormDemo
    /// <code>
    /// public partial class Form1 : Form
    /// {
    ///     private static readonly string querystring = "SELECT * FROM Hospital WHERE {0} ORDER BY AccountID"; // 查询字符串
    ///     private string currentquerystring = "";
    ///     public Form1()
    ///     {
    ///        InitializeComponent();
    ///     }
    ///     private void winFormPager1_PageChanging(object sender, HZ.WinForm.PageChangeEventArgs e)
    ///     {
    ///         Bind();
    ///     }
    ///     private void Bind()
    ///     {
    ///         HZ.Data.PageDescribe pdes = new HZ.Data.PageDescribe(currentquerystring, winFormPager1.PageSize);  //获取当前查询语句
    ///         pdes.CurrentPage = winFormPager1.CurrentPage;// 将当前页从控件读取 赋给页面分页描述类。
    ///         winFormPager1.TotalCount = pdes.TotalCount;// 从页面分页描述获取记录总数
    ///         dataGridView1.DataSource = pdes.GetSpecailPage();// 获取当前页的DataTable
    ///     }
    ///     /// <summary>
    ///     /// 获取查询条件
    ///     /// </summary>
    ///     /// <returns>查询条件</returns>
    ///     private string GetCondition()
    ///     {
    ///           System.Text.StringBuilder condition = new System.Text.StringBuilder(256); // 初始化256*4字节内存
    ///           condition.Append("1=1"); // 辅助
    ///           if (!string.IsNullOrEmpty(txtHosName.Text))
    ///           {
    ///               condition.AppendFormat(" AND HospitalName LIKE '%{0}%'", txtHosName.Text); // 增加查询条件
    ///           }
    ///           return condition.ToString(); // 转换为字符串
    ///      }
    ///      private void btSearch_Click(object sender, EventArgs e)
    ///      {
    ///           winFormPager1.CurrentPage = 1; // 查询时返回第一页
    ///           currentquerystring = string.Format(querystring, GetCondition()); // 将生成的查询语句存入当前查询语句
    ///           Bind(); // 绑定
    ///      } 
    ///      private void Form1_Load(object sender, EventArgs e)
    ///      {
    ///           currentquerystring = string.Format(querystring, GetCondition()); // 将生成的查询语句存入当前查询语句
    ///           Bind(); // 先显示1页结果
    ///       }
    ///  }
    /// </code>
    /// </example>
    [DefaultEvent("PageChanging")]
    [Browsable(true)]
    public partial class WinFormPager : UserControl
    {
        bool isfirstload = false;
        int pagesize = 10;
        /// <summary>
        /// 指定分页的大小
        /// </summary>
        [Browsable(true)]
        [Category("Misc")]
        public int PageSize
        {
            get { return pagesize; }
            set { pagesize = value; }
        }
        int totalcount;
        /// <summary>
        /// 记录总数
        /// </summary>
        public int TotalCount
        {
            get { return totalcount; }
            set
            {
                if (totalcount == value)
                    return;
                totalcount = value;
                if (totalcount == 0)
                {
                    this.Enabled = false;
                }
                else
                    this.Enabled = true;
                // 赋值给最大页
                maxpage = (int)((double)totalcount / pagesize + 0.9999);
                GoToPager.Items.Clear();
                for (int i = 1; i <= maxpage; i++)
                {
                    GoToPager.Items.Add(i);
                }
                if (totalcount == 0)
                {
                    GoToPager.Items.Add(1);
                }
                isfirstload = true;
                GoToPager.Text = "1";
                SetStatus();
                msg.Text = string.Format("当前第{0}页 共{1}页 总计{2}条", currentpage, maxpage, totalcount);
            }
        }

        int currentpage = 1;
        /// <summary>
        /// 当前页码
        /// </summary>
        public int CurrentPage
        {
            get { return currentpage; }
            set { currentpage = value; }
        }

        int maxpage;
        public WinFormPager()
        {
            InitializeComponent();

        }

        /// <summary>
        /// 设置按钮状态
        /// </summary>
        private void SetStatus()
        {
            First.Enabled = currentpage != 1;
            Pre.Enabled = currentpage != 1;
            Next.Enabled = currentpage != maxpage;
            Last.Enabled = currentpage != maxpage;
        }

        public event PageChanged PageChanging = null;

        private void First_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            currentpage = 1;
            GoToPager.Text = currentpage.ToString();
            //  PageChanging.Invoke(sender, new PageChangeEventArgs(currentpage));
        }

        private void Pre_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            currentpage--;
            GoToPager.Text = currentpage.ToString();

            // PageChanging.Invoke(sender, new PageChangeEventArgs(currentpage));
        }

        private void Next_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            isfirstload = false;
            currentpage++;
            GoToPager.Text = currentpage.ToString();

            // PageChanging.Invoke(sender, new PageChangeEventArgs(currentpage));
        }

        private void Last_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            isfirstload = false;
            currentpage = maxpage;
            GoToPager.Text = currentpage.ToString();

            // PageChanging.Invoke(sender, new PageChangeEventArgs(currentpage));
        }

        private void GoToPager_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentpage = GoToPager.SelectedIndex + 1;
            SetStatus();

            msg.Text = string.Format("当前第{0}页 共{1}页 总计{2}条", currentpage, maxpage, totalcount);
            if (!isfirstload)
            {
                PageChanging.Invoke(sender, new PageChangeEventArgs(currentpage));
            }
            else
            {
                isfirstload = false;

            }

        }

        private void GoToPager_DropDownClosed(object sender, EventArgs e)
        {

        }

    }
    /// <summary>
    /// 翻页事件委托
    /// </summary>
    /// <param name="sender">来源</param>
    /// <param name="e">翻页参数</param>
    public delegate void PageChanged(object sender, PageChangeEventArgs e);
    /// <summary>
    /// 翻页事件处理
    /// </summary>
    public class PageChangeEventArgs : EventArgs
    {
        private readonly int _newpage;
        /// <summary>
        /// 新页的页码
        /// </summary>
        public int NewPage
        {
            get { return _newpage; }
        }

        public PageChangeEventArgs(int newpage)
        {
            _newpage = newpage;
        }


    }
}
